package com.company.clientscard.main.widgets.adminForm;

import com.company.clientscard.main.widgets.general.TableManager;
import com.company.clientscard.shared.proxy.ClientProxy;
import com.company.clientscard.shared.proxyManager.ProxyManager;
import com.google.gwt.cell.client.ActionCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ClientsTable extends VerticalPanel {

	private TableManager<ClientProxy> tableManager;

	private CellTable<ClientProxy> table = new CellTable<ClientProxy>();

	Column<ClientProxy, String> nameColumn = new Column<ClientProxy, String>(
			new EditTextCell()) {

		@Override
		public String getValue(ClientProxy object) {
			// TODO Auto-generated method stub
			return object.getName();
		}
	};

	private TextColumn<ClientProxy> codeColumn = new TextColumn<ClientProxy>() {
		@Override
		public String getValue(ClientProxy client) {
			return client.getCode();
		}
	};

	ActionCell<ClientProxy> actionCell = new ActionCell<ClientProxy>("Remove",
			new ActionCell.Delegate<ClientProxy>() {
				@Override
				public void execute(ClientProxy object) {
					// TODO Auto-generated method stub

				}
			});

	Column<ClientProxy, ClientProxy> removeColumn = new Column<ClientProxy, ClientProxy>(
			actionCell) {

		@Override
		public ClientProxy getValue(ClientProxy object) {
			// TODO Auto-generated method stub
			return object;
		}
	};

	private void initTable() {
		nameColumn.setFieldUpdater(new FieldUpdater<ClientProxy, String>() {

			@Override
			public void update(int index, ClientProxy object, String value) {
				// TODO Auto-generated method stub
				// tableManager.update(object, value);
				ProxyManager<ClientProxy> manager = tableManager.getManager();
				ClientProxy editibleObject = manager.getProxyObject(object);
				editibleObject.setName(value);
				manager.update(editibleObject, table);
			}
		});

		table.addColumn(codeColumn, "Code");
		table.addColumn(nameColumn, "Name");
		table.addColumn(removeColumn, "");

		table.setWidth("100%", true);
		table.setColumnWidth(codeColumn, 15.0, Unit.PCT);
		table.setColumnWidth(removeColumn, 10.0, Unit.PCT);

		table.setPageSize(5);

		SimplePager pager = new SimplePager();
		pager.setPageSize(5);
		pager.setDisplay(table);

		setWidth("100%");
		setHorizontalAlignment(ALIGN_CENTER);
		add(table);
		add(pager);

	}

	public ClientsTable(TableManager<ClientProxy> tableManager) {
		this.tableManager = tableManager;
		initTable();

		tableManager.addDataDisplay(table);
	}

	public TableManager<ClientProxy> getTableManager() {
		return tableManager;
	}
}
